#!/usr/bin/env python

import wx


class LoginDialog(wx.Dialog):

  def __init__(self, parent, title, labelText, players, lastLoadedPlayer):
    super(LoginDialog, self).__init__(parent=parent,
        title=title, size=(250, 400))

    self.lastLoadedPlayer = lastLoadedPlayer
    self.players = players
    self.playersList = [ (p, players[p]['password']) for p in players]

    panel = wx.Panel(self)
    vbox = wx.BoxSizer(wx.VERTICAL)

    sb = wx.StaticBox(panel, label='Login')
    sbs = wx.StaticBoxSizer(sb, orient=wx.VERTICAL)
    self.userbox = wx.TextCtrl(panel, -1, size=(200,25), value=self.lastLoadedPlayer)
    self.passbox = wx.TextCtrl(panel, 1, size=(200,25), value=self.players[self.lastLoadedPlayer]['password'])
    self.playersListBox = wx.ListBox(panel, -1, style=wx.LB_EXTENDED, size=(200,170),
                        choices= [p for p,pw in self.playersList])

    self.playersListBox.Bind(wx.EVT_LISTBOX_DCLICK, self.updateToSelectedPlayer)

    self.userbox.Bind(wx.EVT_KEY_UP, self.doOkIfEnter)
    self.passbox.Bind(wx.EVT_KEY_UP, self.doOkIfEnter)
    self.statusLabel = wx.StaticText(panel, label=labelText)
    hbox1 = wx.BoxSizer(wx.VERTICAL)
    hbox1.Add(self.playersListBox, flag=wx.LEFT, border=5)
    hbox1.Add(wx.StaticBox(panel, label='Username'))
    hbox1.Add(self.userbox, flag=wx.LEFT, border=5) 
    hbox1.Add(wx.StaticBox(panel, label='Password'))
    hbox1.Add(self.passbox, flag=wx.LEFT, border=5)
    hbox1.Add(self.statusLabel)
    sbs.Add(hbox1)

    panel.SetSizer(sbs)

    hbox2 = wx.BoxSizer(wx.HORIZONTAL)
    okButton = wx.Button(self, label='Ok')
    closeButton = wx.Button(self, label='Close')
    closeButton.Bind(wx.EVT_BUTTON, self.OnClose)
    hbox2.Add(okButton)
    hbox2.Add(closeButton, flag=wx.LEFT, border=5)

    vbox.Add(panel, proportion=1, flag=wx.ALL|wx.EXPAND, border=5)
    vbox.Add(hbox2, flag= wx.ALIGN_CENTER|wx.TOP|wx.BOTTOM, border=10)

    self.SetSizer(vbox)

    okButton.Bind(wx.EVT_BUTTON, self.OnOK)
    self.userbox.SetFocus()
    self.username = None
    self.password = None
    self.authenticated = False

  def OnClose(self, e):
    self.username = None
    self.password = None
    self.Close()


  def updateToSelectedPlayer(self, e):
    if len(self.playersListBox.GetSelections() )==1:
      index = self.playersListBox.GetSelections()[0]
      newPlayer = self.playersList[index]
      self.userbox.SetValue(newPlayer[0])
      self.passbox.SetValue(newPlayer[1])
    else:
      print 'too lilttle ot roo many selected'


  def doOkIfEnter(self, event):
    keycode = event.GetKeyCode()
    if keycode == 13:
      self.OnOK(event)


  def OnOK(self, event):
    self.username = self.userbox.GetValue()
    self.password = self.passbox.GetValue()
    self.Close()


  def GetResult(self):
    playersDict = self.players
    if self.username not in playersDict:
      playersDict[self.username] = {'password' : self.password}
    playersDict[self.username]['password'] = self.password
    lastLoadedPlayer = self.username
    file = "players = %s\n\nlastLoadedPlayer = '%s'" % (playersDict, lastLoadedPlayer)
    with open('playersInfo.py','w') as f:
      f.write(file)
    return (self.username, self.password)

